home *** CD-ROM | disk | FTP | other *** search
- /* from Dale Schumacher's dLibs */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <ansi.h>
-
- #define _NFILE NFILES
-
- #ifdef __STDC__
- static int _fflush(FILE *);
- #else
- static int _fflush();
- #endif
-
- int fflush(fp)
- register FILE *fp;
- /*
- * implementation note: This function has the side effect of
- * re-aligning the virtual file pointer (in the buffer) with
- * the actual file pointer (in the file) and is therefore used
- * in other functions to accomplish this re-sync operation.
- */
- {
- register int i;
-
- if(fp)
- return(_fflush(fp));
- else
- {
- for(i=0; i<_NFILE; ++i)
- {
- if(_iob[i]._flag & (_IOREAD | _IOWRT | _IORW))
- _fflush(&_iob[i]);
- }
- return(0);
- }
- }
-
- static int _fflush(fp)
- register FILE *fp;
- {
- register int rv = 0;
- register unsigned int f;
- register long offset;
-
- if(fp == NULL)
- return(0);
- f = fp->_flag;
- if (!(f & (_IORW | _IOREAD | _IOWRT))) /* file not open */
- return(0);
- if(fp->_cnt > 0) /* data in the buffer */
- {
- if(f & _IOWRT) /* writing */
- {
- if(lwrite(fp->_file, fp->_base, (long)fp->_cnt)
- != fp->_cnt)
- {
- fp->_flag |= _IOERR;
- rv = EOF;
- }
- }
- else if(f & _IOREAD) /* reading */
- {
- if((offset = -(fp->_cnt)) != 0L)
- if(lseek(fp->_file, offset, 1) < 0)
- rv = EOF;
- else
- fp->_flag &= ~_IOEOF;
- }
- }
- if(f & _IORW)
- fp->_flag &= ~(_IOREAD | _IOWRT);
- fp->_ptr = fp->_base;
- fp->_cnt = 0;
- return(rv);
- }
-